/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package assignment_cp3120.manager;

import assignment_cp3120.auth.Authenticate;
import assignment_cp3120.dataObjects.User;
import assignment_cp3120.manager.createairport.CreateAirportView;
import assignment_cp3120.manager.createflights.CreateFlightsView;
import assignment_cp3120.manager.deleteairport.DeleteAirportView;
import assignment_cp3120.manager.deleteflights.DeleteFlightsView;
import assignment_cp3120.manager.editairport.EditAirportView;
import assignment_cp3120.manager.editflights.EditFlightsView;
import assignment_cp3120.manager.promandgst.PromoGSTView;
import assignment_cp3120.startup.StartupView;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 *
 * @author Administrator
 */
public class ManagerView extends JPanel {
    private JComponent flightsPanel;
    private JComponent airportPanel;
    private JComponent promotionsAndGSTPanel;
    
    private JPanel logoutPanel;
            
    private JButton logoutButton;
    private JTabbedPane tabbedPane;
    private JTabbedPane flightsTP = new JTabbedPane();
    private JTabbedPane airportTP = new JTabbedPane();

    private JPanel previousContent;
    public static Dimension SIZE;

    private CreateFlightsView createFlightsView;
    private EditFlightsView editFlightsView;
    private DeleteFlightsView deleteFlightsView;

    private CreateAirportView createAirportView;
    private EditAirportView editAirportView;
    private DeleteAirportView deleteAirportView;
    
    public ManagerView() {
        createFlightsView = new CreateFlightsView();
        editFlightsView = new EditFlightsView();
        deleteFlightsView = new DeleteFlightsView();
        
        createAirportView = new CreateAirportView();
        editAirportView = new EditAirportView();
        deleteAirportView = new DeleteAirportView();

        tabbedPane = new JTabbedPane(JTabbedPane.SCROLL_TAB_LAYOUT);  
        previousContent = StartupView.getInstance().getContents();
        initFlightsPanel();
        initAirportPanel();
        initPromotionsAndGSTPanel();
        
        logoutPanel = new JPanel();
        logoutButton = new JButton("logout");
        tabbedPane.addTab("Flight Options", null,flightsPanel,
                "Click here to get flight editing options");       
        tabbedPane.addTab("Airport Options", null, airportPanel,
                "Click here to get airport editing options");        
        tabbedPane.addTab("Promotion and GST options", null, promotionsAndGSTPanel,
                "Click here to edit the gst value or edit promotion");
        
        SIZE = tabbedPane.getPreferredSize();System.out.println(SIZE);

        logoutButton.addActionListener(new logoutAL());

        logoutPanel.add(logoutButton);
        //Add the tabbed pane to this panel.
        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        add(tabbedPane);
        add(logoutPanel);
    }    
    private void initFlightsPanel() {
        flightsTP = new JTabbedPane(JTabbedPane.SCROLL_TAB_LAYOUT);           
        flightsPanel = new JPanel();
        
        flightsTP.addTab("Create Flight", null, createFlightsView,
                "Click here to create a new flight");
        flightsTP.addTab("Edit Flight", null, editFlightsView,
                "Click here to edit a flight");        
        flightsTP.addTab("Delete Flight", null, deleteFlightsView,
                "Click here to delete a current flight");
        flightsTP.addChangeListener(new FlightControlTabbedPaneCL());
        flightsPanel.add(flightsTP);
    }
    private void initAirportPanel() {
        airportPanel = new JPanel();
        airportTP = new JTabbedPane(JTabbedPane.SCROLL_TAB_LAYOUT);           
        airportTP.addTab("Create Airport", null, createAirportView,
                "Click here to create a new airport");
        airportTP.addTab("Edit Airport", null, editAirportView,
                "Click here to edit an airport");        
        airportTP.addTab("Delete Airport", null, deleteAirportView,
                "Click here to delete an airport");
        airportTP.addChangeListener(new AirportControlTabbedPaneCL());
        airportPanel.add(airportTP);
    }
    private void initPromotionsAndGSTPanel() {
        promotionsAndGSTPanel = new PromoGSTView();  
    }
    public void logout(){
        StartupView.getInstance().setSubTitle("");
        StartupView.getInstance().clearContents();
        StartupView.getInstance().setContents(previousContent);
        Authenticate.setInstance(new User());
    }
    protected class logoutAL implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            logout();
        }
    }
    public class FlightControlTabbedPaneCL implements ChangeListener {
        public void stateChanged(ChangeEvent changeEvent) {
            JTabbedPane tempPane = (JTabbedPane) changeEvent.getSource();
            int index = tempPane.getSelectedIndex();
            switch (index) {
                case 0:
                    createFlightsView.clearView();
                    break;
                case 1:
                    editFlightsView.clearView();
                    break;
                case 2:
                    deleteFlightsView.clearView();
                    break;
            }
        }
    }
    public class AirportControlTabbedPaneCL implements ChangeListener {
        public void stateChanged(ChangeEvent changeEvent) {
            JTabbedPane tempPane = (JTabbedPane) changeEvent.getSource();
            int index = tempPane.getSelectedIndex();
            switch (index) {
                case 0:
                    createAirportView.clearView();
                    break;
                case 1:
                    editAirportView.clearView();
                    break;
                case 2:
                    deleteAirportView.clearView();
                    break;
            }
        }
    }
    
}
